package com.zy.servlet.shop;

import com.zy.service.AddShopService;
import com.zy.service.impl.AddShopServiceIpml;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;

@WebServlet("/addShop")
public class AddShopServlet extends HttpServlet {
    private AddShopService addShopService = new AddShopServiceIpml();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getSession().getAttribute("loggedIn") == null) {
            response.sendRedirect("login.jsp");
            return;
        }
        String name = request.getParameter("shopName");
        String imageUrl = request.getParameter("imageUrl");
        double price = Double.parseDouble(request.getParameter("shopPrice"));
        String contactPerson = request.getParameter("shopContactPerson");
        String contactNumber = request.getParameter("shopContactPhone");

        boolean success = addShopService.addShop(name, imageUrl, price, contactPerson, contactNumber);

        response.setContentType("text/html;charset=UTF-8");
        if (success) {
            response.getWriter().write("商品添加成功");
        } else {
            response.getWriter().write("商品添加失败");
        }
    }
}
